package com.qys.livesMall.shop.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.shop.modal.entity.Advertisement;
import com.qys.livesMall.shop.modal.vo.AdvertisementVO;
import com.qys.livesMall.shop.service.AdvertisementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 广告表(Advertisement)表控制层
 *
 * @author makejava
 * @since 2023-06-28 09:12:47
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/advertisement")
@Tag(name = "sys-广告后台管理", description = "sys-广告后台管理")
public class AdvertisementController {


    private final AdvertisementService advertisementService;

    /**
     * 分页查询所有数据
     *
     * @param dto 分页对象
     * @return 所有数据
     */
    @PostMapping("/selectAll")
    @Operation(summary = "分页查询广告")
    public R<IPage<AdvertisementVO>> selectAll(@RequestBody BaseQuery dto) {
        IPage<AdvertisementVO> page = advertisementService.page(new Page<>(dto.getPageNum(), dto.getPageSize()), Wrappers.query()).convert(e -> {
            AdvertisementVO advertisementVO = new AdvertisementVO();
            BeanUtils.copyProperties(e, advertisementVO);
            return advertisementVO;
        });
        return R.ok(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "通过主键查询单条广告")
    public R<AdvertisementVO> selectOne(@PathVariable Serializable id) {
        Advertisement advertisement = advertisementService.getById(id);
        if(null == advertisement){
            R.ok();
        }
        AdvertisementVO advertisementVO = new AdvertisementVO();
        BeanUtils.copyProperties(advertisement,advertisementVO);
        return R.ok(advertisementVO);
    }

    /**
     * 立即弹窗
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/unpack/{id}")
    @Operation(summary = "立即弹窗")
    public R<?> unpack(@PathVariable Long id) {
        advertisementService.unpack(id);
        return R.ok();
    }


    /**
     * 新增/修改数据
     *
     * @param advertisementVO 实体对象
     * @return 修改结果
     */
    @PostMapping("/edit")
    @Operation(summary = "新增/修改数据")
    public R<?> editData(@RequestBody AdvertisementVO advertisementVO) {
        Advertisement advertisement = new Advertisement();
        BeanUtils.copyProperties(advertisementVO,advertisement);
        advertisement.setCreateBy(SecurityUtils.getUsername());
        advertisement.setUpdateTime(LocalDateTime.now());
        advertisement.setUpdateBy(SecurityUtils.getUsername());
        return R.ok(advertisementService.saveOrUpdate(advertisement));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/delete")
    @Operation(summary = "删除广告")
    public R<?> delete(@RequestBody List<Long> idList) {
        return R.ok(advertisementService.removeByIds(idList));
    }
}

